package org.tp8.sampleapp.loader.jobs.teacher;

import org.apache.log4j.Logger;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tp8.sampleapp.loader.exc.SampleLoaderException;
import org.tp8.sampleapp.loader.jobs.teacher.model.TeacherCase;
import org.tp8.sampleapp.loader.jobs.teacher.service.intf.TeacherOutputService;
import org.tp8.sampleapp.model.Teacher;

/**
 * The Class AddCaseToBatchTasklet. It retrieves the content from the migration
 * documents and calls the document service.
 * 
 */
@Component("taInsertToSystemTasklet")
public class TAInsertToSystemTasklet extends AbstractTeacherTasklet {

	/** The log. */
	private Logger log = Logger.getLogger(TAInsertToSystemTasklet.class);

	@Autowired
	private TeacherOutputService teacherOutputService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.loader.jobs.AbstractTasklet#executeTasklet(org.
	 * springframework.batch.core.StepContribution,
	 * org.springframework.batch.core.scope.context.ChunkContext)
	 */
	public RepeatStatus executeTasklet(StepContribution contribution,
			ChunkContext chunkContext) throws SampleLoaderException {

		TeacherCase currentCase = getCurrentCase();

		log.debug("Inserting : " + currentCase.getEntity());

		// Insert entity
		Teacher response = teacherOutputService.insert(currentCase.getEntity());

		log.debug("Inserted " + response.getLastName() + " with ID : "
				+ response.getId());

		// remove the case from memory
		removeCurrentCase();

		return RepeatStatus.FINISHED;
	}

}
